
<%@ include file="/common/taglibs.jsp"%>
<!--分页查询共用的页面-->
<%@ include file="/common/common.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>

<script type="text/javascript" src="<%=jsPath%>/jquery/jquery.timers.js"></script><!--定时器-->
<script type="text/javascript" src="<%=jsPath%>/terminalCommand.js"></script><!--终端命令结果查询-->


<script>

    /*
    为防止冲突，修改控件id，修改名称如下：
    原：simNo 现 talkVideoTerminalSimNo
    yuan channel 现：talkVideoTerminalChannel
    原 ：body 现：taklVideoTerminalBody
    原：btnStopTalk      现：btnStopTalkVideoTerminal
    原：btnStartTal    现：btnStartTalkVideoTerminal
    原：commandMsg        现：talkVideoTerminalCommandMsg
    */

    //防止冲突，定义一个终端命令对象
     var tc2TalkVideoTerminal = new TerminalCommand2();
     var timerName = "talkVideoTerminalTimer";
    /////////////////////////////////////////////////////////////////////////////
    
    $().ready(function() {
    console.log("talk video 2");
    
    tc2TalkVideoTerminal.messageDiv = $("#talkVideoTerminalCommandMsg");
    tc2TalkVideoTerminal.timerName=timerName;
    tc2TalkVideoTerminal.commandButton = $("#btnStartTalkVideoTerminal");
     $("#talkVideoTerminalEntityForm").validate(); //初始化验证信息
     

	$("#btnStopTalkVideoTerminal").click(function()
	{
        stopTalk();
	});

    var defaultChannelId = "1";//默认选中
    $("#talkVideoTerminalChannel").fillVehicleChannels({vehicleId:"${vehicleId}",selectedValue:defaultChannelId});
	  Utility.ajaxSubmitForm("talkVideoTerminalEntityForm", {
						 success:function(responseText)
						 {
							   var result = responseText;
								if (result.success) {
									 var commandId = result.data; //下发成功后，获取到命令Id
                                     
									 tc2TalkVideoTerminal.startQueryResult(commandId,null,onTalkCommandSuccess);//命令下发成功,根据命令id,开始尝试获取检索结果
								}
								else {
									$("#talkVideoTerminalCommandMsg").html("提交失败! 错误原因：" + (result.message ? result.message : result.Data));
									//停止所有的在$('body')上名字为timerName的定时器  
									$('body').stopTime (timerName);  
								  }
						 }
	 });
});

    function  startListen() {
        var vehicleId = ${vehicleId};
        var simNo = "${simNo}";
        var channel = $("#talkVideoTerminalChannel").val();
        var ret = window.parent.MyVideoPlayer.startListen(vehicleId, simNo, channel);
        if (ret == 0) {
            //$("input[name='listing']").attr("disabled", true);
            this.isListening = true;
            //$("#listenTip").html(lang.requiring);
            //$("#btnListen").html(lang.stop);
        } else if(ret == -1) {//视频插件未开启
            //$.dialog.tips(parent.lang.vedio_undefined, 1);
            alert("lisetn error");
        }else{
            //一般都不会到此接口
            //除非视频插件没有初始化完成
        }

    }

    function stopListen()
    {
        var vehicleId = ${vehicleId};
        var simNo = "${simNo}";
        var channel = $("#talkVideoTerminalChannel").val();
        var ret = window.parent.MyVideoPlayer.stopListen(simNo);
        if (ret == 0) {
            //$("input[name='listing']").attr("disabled", true);
            //this.isListening = true;
            //$("#listenTip").html(lang.requiring);
            //$("#btnListen").html(lang.stop);
        } else if(ret == -1) {//视频插件未开启
            //$.dialog.tips(parent.lang.vedio_undefined, 1);
            alert("lisetn error");
        }else{
            //一般都不会到此接口
            //除非视频插件没有初始化完成
        }
    }


	function  startTalk() {
	    var vehicleId = ${vehicleId};
	    var simNo = "${simNo}";
        var channel = $("#talkVideoTerminalChannel").val();
        var ret = window.parent.MyVideoPlayer.startTalkback(vehicleId, simNo, channel);
        if (ret == 0) {
            //$("input[name='listing']").attr("disabled", true);
            this.isListening = true;
            //$("#listenTip").html(lang.requiring);
            //$("#btnListen").html(lang.stop);
        } else if(ret == -1) {//视频插件未开启
            //$.dialog.tips(parent.lang.vedio_undefined, 1);
			alert("lisetn error");
        }else{
            //一般都不会到此接口
            //除非视频插件没有初始化完成
        }

    }

    function onStopTalkback()
	{
	    stopListen();

        var vehicleId = ${vehicleId};
        var simNo = "${simNo}";
        var channel = $("#talkVideoTerminalChannel").val();
        var ret = window.parent.MyVideoPlayer.stopTalkback(simNo);
        if (ret == 0) {
            //$("input[name='listing']").attr("disabled", true);
            //this.isListening = true;
            //$("#listenTip").html(lang.requiring);
            //$("#btnListen").html(lang.stop);
        } else if(ret == -1) {//视频插件未开启
            //$.dialog.tips(parent.lang.vedio_undefined, 1);
            alert("lisetn error");
        }else{
            //一般都不会到此接口
            //除非视频插件没有初始化完成
        }
	}

    function stopTalk()
	{
        tc2TalkVideoTerminal.stopQuery();
	    var vehicleId = ${vehicleId};
        var simNo = "${simNo}";
        var channel = 1;
	    var url = "<%=ApplicationPath%>/videoCommand/stopTalk.action";
	    var param = {vehicleId:vehicleId, simNo:simNo,channelId:channel};
	    $.getJSON(url, param, function(result)
		{
		    var message = result.success ? "停止成功": "停止失败";
		    $("#talkVideoTerminalCommandMsg").html(message);
		    if(result.success)
			{
                var commandId = result.data; //下发成功后，获取到命令Id
                tc2TalkVideoTerminal.commandButton = $("#btnStartTalkVideoTerminal");
                tc2TalkVideoTerminal.startQueryResult(commandId,null,onStopTalkback);//命令下发成功,根据命令id,开始尝试获取检索结果
			}

		})

        stopCheckTalkStatus();
	}

    function onTalkCommandSuccess(result)
	{
	    if(result.message != null && result.message.indexOf("成功") < 0)
	        return;
	    startTalk();
        startListen();
        checkTalkStatus();
	}
	var talkTimerName = "talkTimer";
    var talkStatus = null;
	function checkTalkStatus()
	{
	    var simNo = $("#talkVideoTeminalSimNo").val();////simNo控件信息
	    while(simNo.length < 12)
	        simNo = "0" + simNo;
	    var channel = $("#talkVideoTerminalChannel").val();
        $('#taklVideoTerminalBody').everyTime('500ms', talkTimerName,function(){
            var talkWindow = window.parent.MyVideoPlayer.getVideoHandler().ttxPlayer.getTalkbackWindow();
            if(talkWindow.talkStatus == "talking")
			{
			    $("#talkIconContainer").css("color", "green");
			    $("#talkMessage").html("对讲连接成功，请讲话...");
			}else if( talkWindow.talkStatus =="talkStopped")
            {
                $("#talkIconContainer").css("color", "red");

                $("#talkIcon").attr("class", "fa fa-microphone-slash");

                $("#talkMessage").html("对讲连接中断");
                $('#taklVideoTerminalBody').stopTime (talkTimerName);
            }

        });
	}

	function stopCheckTalkStatus()
	{
        $("#talkIconContainer").css("color", "gray");

        $("#talkIcon").attr("class", "fa fa-microphone");

        $("#talkMessage").html("对讲连接中断");
        $('#taklVideoTerminalBody').stopTime (talkTimerName);
	}


    /**
     * 当easyui的dialog 被关闭的时候，会自动调用此方法，停止监听
     * @returns {boolean}
     */
    function onWindowBeforeClose()
    {
        stopTalk();
        stopListen();
        console.log("监听窗口被关闭，自动停止对讲");
        return true;
    }


</script>
 <BODY id="taklVideoTerminalBody">
	<form id="talkVideoTerminalEntityForm" name="talkVideoTerminalEntityForm" 
			action='<%=ApplicationPath%>/videoCommand/talk.action' method="POST">
				
        <input type="hidden"  name="vehicleId"  id="talkVideoTerminalVehicleId" value="${vehicleId}"/>
		<input type="hidden"  name="simNo"  id="talkVideoTeminalSimNo" value="${simNo}"/>
  <table width="100%"  class="TableBlock">
					<tbody><tr>
						<td colspan="2" style="font-weight: bold; background: #EFEFEF;" height="25">监听信息
						<span style="color:red;background:blue;">${message}</span>
						</td>
					</tr>

					<tr>
						<td align="center">
							<select id="talkVideoTerminalChannel" name="channel">
							</select>
						</td>
					</tr>
					<tr>
						<td colspan="2" align="center">
							<span id="talkIconContainer" style="font-size: 48px; color: darkgray;">
								  <i id="talkIcon" class="fa fa-microphone"></i>
								</span>
							<div id="talkMessage">请点击对讲按钮开始对讲</div>
						</td>
					</tr>
					<tr>


						<td colspan=2 align="center">
							<button id="btnStartTalkVideoTerminal" type="submit" class="button green medium sendjson" ><i class="fa fa-microphone" style="margin-right:3px;"></i> 开始对讲</button>

							<button id="btnStopTalkVideoTerminal" type="button" class="button red medium " ><i class="fa fa-microphone-slash" style="margin-right:3px;"></i> 停止对讲</button>


							<span id="talkVideoTerminalCommandMsg" class="commandMsg"></span></td>
						
					</tr>

				
					
				</tbody></table>
 </BODY>
</HTML>
